package com.androidcourse.shounews.Activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.androidcourse.shounews.Fragment.AddNewsFragment;
import com.androidcourse.shounews.Fragment.BottomFragment;
import com.androidcourse.shounews.Fragment.FirstFragment;
import com.androidcourse.shounews.Fragment.NewsViewFragment;
import com.androidcourse.shounews.Fragment.PersonFragment;
import com.androidcourse.shounews.R;

public class MainActivity extends FragmentActivity  {
   private FirstFragment firstFragment=new FirstFragment();
   private BottomFragment  bottomFragment=new BottomFragment();
   private AddNewsFragment addNewsFragment =new AddNewsFragment();
   private PersonFragment personFragment=new PersonFragment();
   private NewsViewFragment newsViewFragment=new NewsViewFragment();

   private RadioGroup radioGroup;
    //加载初始页面的fragment
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_page);
        getSupportFragmentManager().beginTransaction().add(R.id.bottom_fragment,bottomFragment).commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.content_layout,firstFragment).commit();
    }

    /**
     * 便于bottom fragment调用切换
     * @param group
     * @param checkedId
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton message=findViewById(R.id.bottom_news);
        message.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.message_normal_34),null,null);
        RadioButton add=findViewById(R.id.bottom_add);
        add.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.add_normal_34),null,null);
        RadioButton person=findViewById(R.id.bottom_person);
        person.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.account_normal_34),null,null);
        if(checkedId==R.id.bottom_news){
            message.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.message_pressed_34),null,null);
            getSupportFragmentManager().beginTransaction().replace(R.id.content_layout,firstFragment).commit();
        }else if(checkedId==R.id.bottom_add){
            add.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.add_pressed_34),null,null);
            getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, addNewsFragment).commit();
        }else if(checkedId==R.id.bottom_person){
            person.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.account_pressed_34),null,null);
            getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, personFragment).commit();
        }
    }

    /**
     * @param bundle firstFragment传item中的值给newsViewFragment
     */
    public void openWebFragment(Bundle bundle) {
        getSupportFragmentManager().beginTransaction().hide(bottomFragment).commit();
        //不可少，否则会重复一个页面
        newsViewFragment=new NewsViewFragment();
        newsViewFragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_layout,newsViewFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    /**
     * 返回主页，切换fragment
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    public void backFirst(){
        getSupportFragmentManager().beginTransaction().replace(R.id.content_layout,firstFragment).commit();
        getSupportFragmentManager().beginTransaction().show(bottomFragment).commit();
        RadioButton message=findViewById(R.id.bottom_news);
        message.setCompoundDrawablesWithIntrinsicBounds(null,getDrawable(R.drawable.message_pressed_34),null,null);
    }
}